package com.ddyx.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.ddyx.entity.wxPortalPic;
import com.ddyx.mapper.wxPortalPicMapper;
import com.ddyx.service.IWxPortalPicService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;

//import ddyx.commonfile.CommonFile;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WxPortalPicServiceImpl implements IWxPortalPicService{

	@Value("${pic.basepath}")
	private String basePath;
	
	@Resource
	wxPortalPicMapper portalPicMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm deletePortalPic(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map<String, Object> parMap = (Map<String, Object>)param.getData();
			int index = portalPicMapper.deletePortalPic(parMap);
			if(index < 1) {
				result.setStatus(false);
				result.setMessage("删除异常");
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("删除异常");
			log.error("删除信息异常" + e);
		}
		return result;
	}

	@Override
	public ResponseForm insertPortalPic(HttpServletRequest request) {
		ResponseForm result = new ResponseForm();
		try {
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			if (multipartResolver.isMultipart(request)) {
				if(insertPicIsSuccess(request) < 1) {
					result.setStatus(false);
					result.setMessage("上载失败");
					return result;
				}
				String storeId = request.getParameter("storeId"); 
				String path = basePath + "/store/" + storeId + "/";
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				Iterator<String> iter = multiRequest.getFileNames();
				while (iter.hasNext()) {
					List<MultipartFile> files = multiRequest.getFiles(iter.next());
					for (MultipartFile file : files) {
						String field = file.getName();
						String fileName = file.getOriginalFilename();
						System.out.println(fileName + "  " + field);
//						CommonFile.upload(file.getBytes(),"jpg",path);
					}
				}
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("上载错误" + e);
			return result;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectPortalPic(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map<String, Object> parMap = (Map<String, Object>)param.getData();
			List<wxPortalPic> list = portalPicMapper.selectPortalPic(parMap);
			result.setData(list);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询异常");
			log.error("查询异常" + e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm updatePortalPic(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map<String, Object> parMap = (Map<String, Object>)param.getData();
			int index = portalPicMapper.updatePortalPic(parMap);
			if(index < 1) {
				result.setStatus(false);
				result.setMessage("修改异常");
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("修改异常");
			log.error("修改异常" + e);
		}
		return result;
	}
	
	private int insertPicIsSuccess(HttpServletRequest request) throws Exception{
		Map<String,Object> param = new HashMap<>();
		String storeId = request.getParameter("storeId");
		String createUser = request.getParameter("createUser");
		param.put("storeId", storeId);
		param.put("createUser", createUser);
		param.put("createTime", DateUtil.format_ymd_hms.format(new Date()));
		return portalPicMapper.insertPortalPic(param);
	}
}
